
Private Const TEST_SHEET_NAME As String = "TestSheetLog" 

'=======================================
' 执行所有测试 case
'=======================================
Private Sub test_ALL_Case()
    ' InitBySheetName 测试
    test_InitBySheetName
    ' info 测试
    test_info
    ' error 测试
    test_error
    ' warn 测试
    test_warn
    ' writeLog 测试
    test_writeLog
    ' init 测试: 不需要清空 sheet
    test_Init_Not_NeedClear
End Sub

'=======================================
' 其他方法
'=======================================
' 获取测试的 sheet 对象
Private Function getTestSheet() As Worksheet
    If Not UtilsWorkbook.hasSheet(ThisWorkbook, TEST_SHEET_NAME) Then
        Set getTestSheet = UtilsWorkbook.addLastSheet(ThisWorkbook, TEST_SHEET_NAME)
    Else
        Set getTestSheet = ThisWorkbook.Sheets(TEST_SHEET_NAME)
    End If
End Function

' 获取测试的 sheet 对象，并且写入一些随机数据
' @param writeRowCount 需要写入的数据的行数
Private Function getTestSheetAndWriteSomething(ByRef writeRowCount As Integer) As Worksheet
    If Not UtilsWorkbook.hasSheet(ThisWorkbook, TEST_SHEET_NAME) Then
        Set getTestSheetAndWriteSomething = UtilsWorkbook.addLastSheet(ThisWorkbook, TEST_SHEET_NAME)
    Else
        Set getTestSheetAndWriteSomething = ThisWorkbook.Sheets(TEST_SHEET_NAME)
    End If

    Dim i As Integer
    For i = 1 To writeRowCount
        getTestSheetAndWriteSomething.Cells(i,1) = "xxxxx"
    Next
End Function

' 删除测试的 sheet 对象
Private Sub delTestSheet()
    If Not UtilsWorkbook.hasSheet(ThisWorkbook, TEST_SHEET_NAME) Then
        UtilsWorkbook.deleteSheet ThisWorkbook, TEST_SHEET_NAME
    End If
End Sub

'=======================================
' 各方法的 case
'=======================================
' InitBySheetName 测试
Private Sub test_InitBySheetName()
    delTestSheet

    ' --------------------
    Dim logger As SheetLogger
    Set logger = New SheetLogger

    Dim wb As Workbook
    Set wb = ThisWorkbook
    logger.InitBySheetName wb, TEST_SHEET_NAME

    logger.info "xxxxxx"

    ' --------------
    Dim sh As Worksheet
    Set sh = wb.Sheets(TEST_SHEET_NAME)
    Debug.Assert InStr(sh.Cells(1, 1), "INFO") <> 0
    Debug.Assert sh.Cells(1, 2) = "xxxxxx"
End Sub

' info 测试
Private Sub test_info()
    Dim logger As SheetLogger
    Set logger = New SheetLogger

    Dim sh As Worksheet
    Set sh = getTestSheet

    logger.Init sh
    logger.info "test_info"

    ' --------------
    Debug.Assert InStr(sh.Cells(1, 1), "INFO") <> 0
    Debug.Assert sh.Cells(1, 2) = "test_info"
End Sub

' error 测试
Private Sub test_error()
    Dim logger As SheetLogger
    Set logger = New SheetLogger

    Dim sh As Worksheet
    Set sh = getTestSheet

    logger.Init sh
    logger.error "test_error"

    ' --------------
    Debug.Assert InStr(sh.Cells(1, 1), "ERROR") <> 0
    Debug.Assert sh.Cells(1, 2) = "test_error"
End Sub

' warn 测试
Private Sub test_warn()
    Dim logger As SheetLogger
    Set logger = New SheetLogger

    Dim sh As Worksheet
    Set sh = getTestSheet

    logger.Init sh
    logger.warn "test_warn"

    ' --------------
    Debug.Assert InStr(sh.Cells(1, 1), "WARN") <> 0
    Debug.Assert sh.Cells(1, 2) = "test_warn"
End Sub

' writeLog 测试
Private Sub test_writeLog()
    Dim logger As SheetLogger
    Set logger = New SheetLogger

    Dim sh As Worksheet
    Set sh = getTestSheet

    logger.Init sh
    logger.writeLog "newLevel", "test_writeLog"

    ' --------------
    Debug.Assert InStr(sh.Cells(1, 1), "newLevel") <> 0
    Debug.Assert sh.Cells(1, 2) = "test_writeLog"
End Sub

' init 测试: 不需要清空 sheet
Private Sub test_Init_Not_NeedClear()
    Dim logger As SheetLogger
    Set logger = New SheetLogger

    Dim sh As Worksheet
    Set sh = getTestSheetAndWriteSomething(10)

    logger.Init sh, False
    logger.info "test_info"

    ' --------------
    Debug.Assert InStr(sh.Cells(11, 1), "INFO") <> 0
    Debug.Assert sh.Cells(11, 2) = "test_info"
End Sub